<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->

        <treeTools style="width:100%" :tree-node="company" :is-root="true" @addpart="addpart" />

        <!-- 下划线 -->
        <hr>
        <!-- 部门数据 -->
        <el-tree :data="depts" :props="defaultProps" default-expand-all>
          <template #default="scoped">
            <!-- 树形组件 -->
            <treeTools
              style="width:100%"
              :tree-node="scoped.data"
              @del="getDepartments"
              @addpart="addpart"
              @editDepts="editDepts"
            />
          </template>
        </el-tree>
        <AddDept ref="adddept" :showdia.sync="showdia" :tree-node="treeNode" @addpart="getDepartments" />
      </el-card>
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { listToTreeData } from '@/utils/index'
import AddDept from '@/views/departments/components/add-dept.vue'
export default {
  components: {
    treeTools, AddDept
  },
  data() {
    return {
      depts: [{ name: '总裁办', manager: '曹操', children: [{ name: '董事会', manager: '曹丕' }] },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      showdia: false,
      treeNode: {},
      node: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const { depts } = await getDepartments()
      // console.log(res)
      // this.depts = depts
      this.depts = listToTreeData(depts, '')
    },
    // 子传父 监听子组件的打开弹出框
    addpart(treeNode) {
      this.treeNode = treeNode
      this.showdia = true
    },
    // 编辑部门 回现数据
    async editDepts(treeNode) {
      // 通过refs调用子组件方法
      await this.$refs.adddept.getDepartDetail(treeNode.id)
      this.treeNode = treeNode

      // 打开弹层
      this.showdia = true
    }
  }

}
</script>

<style scoped>
.tree-card {
  /* padding: 30px  140px; */
  font-size:14px;
}
.xia{
  cursor: pointer;
}
.title-head{
  font-weight: 700;
}
</style>
